// pages/template/queryLogistics.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [], //
    activeIndex: 0,
    page: 1, 
    size: 20,
    name: '',
    totalPages: 0,
    sendNo: '',
    orderId: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    if (options.orderId != undefined){
      _this.setData({
        orderId: options.orderId
      })
    }
    _this.queryLogistics();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //console.log("上拉刷新")
    let _this = this;
    _this.setData({
      page: 1,
      list: [],
    })
    wx.showLoading({
      title: '刷新中',
    })
    _this.queryLogistics(function () {
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("下拉加载")
    let _this = this;
    if (_this.data.page < _this.data.totalPages) {
      _this.setData({
        page: _this.data.page + 1,
      })
      wx.showLoading({
        title: '加载中',
      })
      this.queryLogistics(function () {
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },
  
  queryLogistics(callback){
    let _this = this;
    Api.queryLogistics(_this.data.page, _this.data.size, _this.data.name).then( res => {
      console.log(res)
      _this.setData({
        list: _this.data.list.concat(res.data.data.dataList),
        totalPages: res.data.data.totalPages
      });
      (callback && (typeof callback) === "function") && callback();
      //console.log(_this.data.list)
    })
  },

  selectClick(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
    //console.log(index)
    _this.setData({
      activeIndex: index
    })
  },

  //获取订单号
  getCode(){
    let _this = this;
    wx.scanCode({
      //onlyFromCamera: true,
      success(res) {
        _this.setData({
          sendNo: res.result
        })
      }
    })
  },
  submintBtn(){
    let _this = this;
    if (!_this.data.sendNo){
      wx.showToast({
        title: '订单号不能为空',
        icon: 'none',
        duration: 1000
      })
    }
    let data = {
      orderId: _this.data.orderId,
      postCompanyId: _this.data.list[_this.data.activeIndex].id,
      sendNo: _this.data.sendNo,
    }
    Api.seedGoods(data).then( res => {
      //console.log(res);
      wx.showToast({
        title: '发货成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(function(){
        let pages = getCurrentPages(); // 获取页面栈
        let currPage = pages[pages.length - 1]; // 当前页面
        let prevPage = pages[pages.length - 2]; // 上一个页面
        prevPage.setData({
          seedStatus: true,
        })
      },2000);
    })
  },

})